/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.facilscrum.form.userstories;

import org.facilscrum.beans.Equipos;
import org.facilscrum.beans.Equiposusuarios;
import org.facilscrum.beans.Proyectos;
import org.facilscrum.beans.Sprint;
import org.facilscrum.beans.Userstories;
import org.facilscrum.beans.Usuarios;
import org.facilscrum.controller.EquiposController;
import org.facilscrum.controller.EquiposusuariosController;
import org.facilscrum.controller.ProyectosController;
import org.facilscrum.controller.SprintController;
import org.facilscrum.controller.UserstoriesController;
import org.facilscrum.controller.UsuariosController;
import org.facilscrum.generales.Mensajeria;
import org.facilscrum.sesion.MySesion;

/**
 *
 * @author avbravo
 */
public class UserstoriesForm extends javax.swing.JInternalFrame {

    EquiposusuariosController equiposusuariosController = new EquiposusuariosController();
    ProyectosController proyectosController = new ProyectosController();
    SprintController sprintController = new SprintController();
    Sprint sprint = new Sprint();
    UsuariosController usuariosController = new UsuariosController();
    UserstoriesController userstoriesController = new UserstoriesController();
    Userstories userstories = new Userstories();

    /**
     * Creates new form UserstoriesForm
     */
    public UserstoriesForm() {
        initComponents();
        jButtonGuardar.setEnabled(false);

        if (!MySesion.getSeleccionoProyectoTrabajo()) {
            Mensajeria.MensajeWarning("Debe indicar el proyecto de trabajo");
        } else {
            if (cargarComboUsuarios()) {
                jButtonGuardar.setEnabled(true);
            }
            jTextFieldProyecto.setText(MySesion.getProyectos().getProyecto());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldProyecto = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldNombre = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldImportancia = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaComoProbarlo = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaNotas = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        jComboBoxUsuarios = new javax.swing.JComboBox();
        jButtonGuardar = new javax.swing.JButton();
        jButtonCerrar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jComboBoxPuntos = new javax.swing.JComboBox();

        setClosable(true);
        setTitle("User Stories");

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setText("Proyecto");

        jTextFieldProyecto.setEnabled(false);

        jLabel3.setText("Nombre");

        jLabel4.setText("Importancia(0-100)");

        jLabel6.setText("Como probarlo");

        jTextAreaComoProbarlo.setColumns(20);
        jTextAreaComoProbarlo.setRows(5);
        jScrollPane1.setViewportView(jTextAreaComoProbarlo);

        jLabel7.setText("Notas");

        jTextAreaNotas.setColumns(20);
        jTextAreaNotas.setRows(5);
        jScrollPane2.setViewportView(jTextAreaNotas);

        jLabel8.setText("Usuario");

        jButtonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/jscrum/gif/save_icon.gif"))); // NOI18N
        jButtonGuardar.setToolTipText("Guardar");
        jButtonGuardar.setEnabled(false);
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/jscrum/gif/return.gif"))); // NOI18N
        jButtonCerrar.setToolTipText("Cerrar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        jLabel5.setText("Puntos");

        jComboBoxPuntos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "8", "13", "20", "40", "100" }));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldProyecto)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldImportancia, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(28, 28, 28)
                                .addComponent(jLabel5)
                                .addGap(4, 4, 4)
                                .addComponent(jComboBoxPuntos, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(64, 64, 64)
                                .addComponent(jComboBoxUsuarios, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56)
                .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 148, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldProyecto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldImportancia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBoxPuntos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(jLabel7))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(11, 11, 11)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jComboBoxUsuarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButtonGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(26, 26, 26))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButtonCerrar)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 17, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed

        try {
            if (!guiToData()) {
                return;
            }
            if (userstoriesController.insert(userstories)) {
                Mensajeria.Mensaje("Se guardo exitosamente");
                jTextAreaComoProbarlo.setText("");
                jTextAreaNotas.setText("");
                jTextFieldImportancia.setText("");
                jTextFieldNombre.setText("");


            } else {
                Mensajeria.MensajeWarning("No se pudo guardar " + userstoriesController.getMensaje());
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "agregar()");
        }


    }//GEN-LAST:event_jButtonGuardarActionPerformed

    private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonCerrarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JComboBox jComboBoxPuntos;
    private javax.swing.JComboBox jComboBoxUsuarios;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaComoProbarlo;
    private javax.swing.JTextArea jTextAreaNotas;
    private javax.swing.JTextField jTextFieldImportancia;
    private javax.swing.JTextField jTextFieldNombre;
    private javax.swing.JTextField jTextFieldProyecto;
    // End of variables declaration//GEN-END:variables

    private Boolean cargarComboUsuarios() {
        try {

            jComboBoxUsuarios.removeAllItems();
            String sql = "idequipo='" + MySesion.getProyectos().getIdequipo() + "'";
            if (equiposusuariosController.query(sql)) {
                for (Equiposusuarios eu : equiposusuariosController.getEquiposusuarioslist()) {
                    jComboBoxUsuarios.addItem(eu.getUsuarios().getNombre() + " - " + eu.getUsuarios().getCedula());
                }
                return true;
            } else {
                Mensajeria.MensajeWarning("No hay usuarios asignados al equipo de este proyecto");
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "cargarComboUsuarios() ");
        }
        return false;
    }

    private Boolean guiToData() {
        try {

            if (jTextFieldNombre.getText() == null || jTextFieldNombre.getText().equals("")) {
                Mensajeria.Mensaje("El nombre  no debe estar vacio");
                jTextFieldNombre.requestFocus();
                return false;
            }
            if (jTextFieldImportancia.getText() == null || jTextFieldImportancia.getText().equals("")) {
                Mensajeria.Mensaje("La importancia no debe estar vacio");
                jTextFieldImportancia.requestFocus();
                return false;
            }


            if (jTextAreaComoProbarlo.getText() == null || jTextAreaComoProbarlo.getText().equals("")) {
                Mensajeria.Mensaje("Como probarlo  no debe estar vacio");
                jTextAreaComoProbarlo.requestFocus();
                return false;
            }
            if (jTextAreaNotas.getText() == null || jTextAreaNotas.getText().equals("")) {
                Mensajeria.Mensaje("Notas  no debe estar vacio");
                jTextAreaNotas.requestFocus();
                return false;
            }
            if (Integer.parseInt(jTextFieldImportancia.getText()) <= 0 || Integer.parseInt(jTextFieldImportancia.getText()) > 100) {
                Mensajeria.MensajeWarning("La importancia debe estar entre 1 y 100");
                jTextFieldImportancia.requestFocus();
                return false;
            }



            userstories.setImportancia(Integer.parseInt(jTextFieldImportancia.getText()));
            userstories.setComoprobarlo(jTextAreaComoProbarlo.getText());
            userstories.setNotas(jTextAreaNotas.getText());
            userstories.setNombre(jTextFieldNombre.getText());
            userstories.setFecha(MySesion.getFechausername());
            userstories.setAsignado("no");
            userstories.setIdproyecto(MySesion.getProyectos().getIdproyecto());

            int k = jComboBoxUsuarios.getSelectedIndex();
            if (k != -1) {
                //  Usuarios usuarios = usuariosController.getUsuarioslist().get(k);
                Equiposusuarios e = equiposusuariosController.getEquiposusuarioslist().get(k);
                userstories.setUsername(e.getUsername());
            } else {
                Mensajeria.Mensaje("No se ha seleccionado ningun usuario");
                return false;
            }
            int m = jComboBoxPuntos.getSelectedIndex();
            if (m != -1) {
                userstories.setPuntos(Integer.parseInt(jComboBoxPuntos.getSelectedItem().toString()));
            } else {
                Mensajeria.Mensaje("No se ha seleccionado los puntos");
                return false;
            }

            return true;
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "guiToData");
        }
        return false;
    }
}
